package com.fd.servlet;

import com.fd.service.impl.StudentServiceImpl;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class SearchServlet extends HttpServlet {
//    @Autowired
//    private IStudentService studentService;
//    IStudentService studentService = new StudentService();

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        StudentServiceImpl studentService = getiStudentService();
//        String searchVlaue = req.getParameter("searchVlaue");
        List query = studentService.getAll();
        req.setAttribute("student", query);
        req.getRequestDispatcher("/WEB-INF/html/data.jsp").forward(req, resp);
    }

    private static StudentServiceImpl getiStudentService() {
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext-jdbc-ann.xml");
        StudentServiceImpl studentService = (StudentServiceImpl) classPathXmlApplicationContext.getBean(com.fd.service.StudentService.class);
        return studentService;
    }
}